package com.hahaduo.bee.web.filter;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthorityChecker implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {

		if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
			throw new ServletException("OncePerRequestFilter just supports HTTP requests");
		}
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		
		
		HttpSession session = httpRequest.getSession();
		
		System.out.println("isNew: " + session.isNew());
		System.out.println("CreationTime: "+ session.getCreationTime());
		
		
		Enumeration<String> attNames = session.getAttributeNames();
		while (attNames.hasMoreElements()) {
			String sn = (String) attNames.nextElement();
			System.out.println(sn + " : " + session.getAttribute(sn));
		}
		System.out.println("Session ID: " + session.getId());
		
		// Proceed without invoking this filter...
		filterChain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}


	

}
